package com.changdu.seatunnel.admin.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.changdu.seatunnel.admin.mapper.sql.RepositoryMapperFactory;
import com.changdu.seatunnel.admin.pojo.SqlReq;
import com.changdu.seatunnel.admin.util.Throwabler;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;


@RestController
@RequestMapping("/mysql")
@RequiredArgsConstructor
public class MysqlController {

	@Autowired
	private RepositoryMapperFactory repositoryMapperFactory;
	
    @PostMapping("/commit")
    public void commit(@RequestBody SqlReq sqlReq, HttpServletResponse response) throws IOException {
    	try {
			repositoryMapperFactory.executeSql(sqlReq.getBody());
		} catch (Exception e) {
			String stackTrace = Throwabler.buildConsoleStack(e);
			response.getOutputStream().write(stackTrace.getBytes());
			response.getOutputStream().flush();
		}
    }

}
